package com.emenu;

import java.io.File;

import com.emenu.Login.loginTask;
import com.emenu.application.StaticObject;
import com.emenu.bean.User;
import com.emenu.common.Common;
import com.emenu.common.GetWebServiceData;
import com.emenu.common.JsonUntil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class FunChgPW extends Fragment {
	public EditText oldpw;
	public EditText newpw1;
	public EditText newpw2;
	public Button chgedit;

	public StaticObject SO;
	Context cn;
	public static final String SET_NAME = "jpSetFile";
	private String url;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		cn = this.getActivity();
		SO = (StaticObject) activity.getApplication();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.funchgpw, container, false);

		findViewsById(v);
		setListener();

		SharedPreferences settings = cn.getSharedPreferences(SET_NAME, 0);
		url = settings.getString("url", "");
		return v;
	}

	private void findViewsById(View v) {
		oldpw = (EditText) v.findViewById((R.id.funoldpw));
		newpw1 = (EditText) v.findViewById((R.id.funnewpw1));
		newpw2 = (EditText) v.findViewById((R.id.funnewpw2));
		chgedit = (Button) v.findViewById((R.id.funchgedit));
	}

	/** 设置监听器 */
	private void setListener() {
		chgedit.setOnClickListener(chgeditListen);
	}

	private OnClickListener chgeditListen = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String userpw = oldpw.getText() + "";
			if (userpw.equals(SO.getStaticUserPW())) {
				new chgPWTask().execute((newpw1.getText() + "").trim(),
						(newpw2.getText() + "").trim());
			} else {
				Common.alertFinish(cn, "旧密码输入不正确！");
			}

		}
	};

	/**
	 * 异步修改密码
	 */
	public class chgPWTask extends AsyncTask<String, Void, Boolean> {

		// 执行onPreExecute()完后立即调用
		@Override
		protected Boolean doInBackground(String... params) {
			// 调用webService

			String pw = JsonUntil.getMd5Value(params[0]);

			Boolean isCheck = false;

			if (params[0].equals(params[1])) {

				String tempstr = GetWebServiceData
						.ChgPW(url,"update sys_user set password='" + pw
								+ "' where code='" + SO.getStaticUser() + "'");
				
			 
				if (tempstr.length() > 0) {
					isCheck = false;
				} else {
					isCheck = true;
				}

			} else {
				Common.alertFinish(cn, "两次新密码必须一致！");
			}

			return isCheck;
		}

		// doInBackground后执行
		// result 的得自于doInBackgroun
		@Override
		protected void onPostExecute(Boolean result) {

			if (result) {
				SO.setStaticUserPW(String.valueOf(newpw1.getText()).trim());
				Common.alertFinish(cn, "修改密码成功！");
			} else {
				Common.alertFinish(cn, "修改密码失败，请重试！");
			}
		}

	}
}
